package com.resjz.milddog.Implement.dept;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.resjz.milddog.Interface.dept.DeptInterface;
import com.resjz.milddog.entity.Dept.DeptEntity;
import com.resjz.milddog.service.dept.DeptService;
import com.resjz.milddog.vo.dept.DeptVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@org.apache.dubbo.config.annotation.DubboService(version = "${dubbo.service.version}",retries = 2,
        methods ={@Method(name ="saveDept",timeout = 10000,retries = 0)})
@Slf4j
public class DeptInterfaceImpl implements DeptInterface {
    @Autowired
    private DeptService deptService;
    public Boolean saveDept(DeptVo deptVo){
        if(StringUtils.isEmpty(deptVo.getDeptId()))
            return deptService.save(BeanUtil.toBean(deptVo, DeptEntity.class));
        else
            return deptService.updateById(BeanUtil.toBean(deptVo, DeptEntity.class));
    }

    @Override
    public List<DeptVo> getDeptArr() {
        List<DeptEntity> deptlist = deptService.list(new QueryWrapper<DeptEntity>()
                .eq("del_flag",0));
        List<DeptVo> backlist=new ArrayList<>();
        Map<Long, List<DeptEntity>> collect = deptlist.stream().collect(Collectors.groupingBy(DeptEntity::getParentId));
        List<DeptEntity> templist = collect.get(0L);
        for (DeptEntity deptEntity : templist) {
            DeptVo deptVo = new DeptVo();
            deptVo.setName(deptEntity.getName());
            deptVo.setDeptId(deptEntity.getDeptId());
            deptVo.setParentId(deptEntity.getParentId());
            deptVo.setSupername("");
            List<DeptVo> sublist =  findchild(collect,deptEntity.getDeptId(),deptEntity.getName());
            if (sublist.size() > 0) {
                deptVo.setChildren(sublist);
            }
            backlist.add(deptVo);
        }
        return backlist;
    }
    private List<DeptVo> findchild(Map<Long, List<DeptEntity>> collect,Long pid ,String pname){
        List<DeptEntity> templist = collect.get(pid);
        List<DeptVo> dglist = new ArrayList<>();
        if(templist==null||templist.size()<=0){
            return dglist;
        }else{
            for (DeptEntity deptEntity : templist) {
                DeptVo deptVo = new DeptVo();
                deptVo.setName(deptEntity.getName());
                deptVo.setDeptId(deptEntity.getDeptId());
                deptVo.setParentId(deptEntity.getParentId());
                deptVo.setSupername(pname);
                List<DeptVo> sublist =  findchild(collect,deptEntity.getDeptId(),deptEntity.getName());
                if (sublist.size() > 0) {
                    deptVo.setChildren(sublist);
                }
                dglist.add(deptVo);
            }
            return  dglist;
        }
    }

    @Override
    public Boolean isHasDeptById(Long deptId) {
        int count = deptService.count(new QueryWrapper<DeptEntity>()
                .eq("dept_id", deptId)
                .eq("del_flag", 0));
        return count>0?true:false;
    }

    @Override
    public DeptVo getDeptById(Long deptId) {
        DeptEntity byId = deptService.getById(deptId);
        if(BeanUtil.isNotEmpty(byId)) return BeanUtil.toBean(byId,DeptVo.class);
        return null;
    }

    @Override
    public Boolean hasSubDept(Long deptId) {
        int parent_id = deptService.count(new QueryWrapper<DeptEntity>()
                .eq("parent_id", deptId)
                .eq("del_flag", 0));
        if(parent_id>0) return true;
        return false;
    }

    @Override
    public Boolean delDept(Long deptId,Boolean islogicdel) {
        if(islogicdel){
            DeptEntity byId = deptService.getById(deptId);
            byId.setDelFlag(-1);
            return deptService.updateById(byId);
        }else
            return deptService.removeById(deptId);
    }
}
